#!/bin/bash
#version：1.2
#kong
#serviceName:服务名
#nameSpaces：命名空间
#domainName：域名
#urlPath: 子目录
#is_path：URL是否有目录
#Yml_path：yaml文件保存路径
set -x

serviceName=$1
nameSpaces=$2
deployEnv=$3
domainName=$4
urlPath=$5
fjfuyu_net=$6
Yml_path=$7

urlPath2="/cdn/"
serviceName2="fuyu-iots-card-cdn"


if [ ! $# == 7 ]; then

  echo "Usage: $0 serviceName nameSpaces deployEnv domainName urlPath fjfuyu_net Yml_path"

  exit

fi

  
if [ n${fjfuyu_net} == n"true" ] ;then
  if [ n${deployEnv} == n"prod" ];then
    domainName="${domainName}"
  elif [ n${deployEnv} == n"pre" ];then
    domainName="stag.${domainName}"
  elif [ n${deployEnv} == n"dev" ];then
    domainName="test.${domainName}"
  fi
  cert_secretName=`echo ${domainName#*.}`
  cert_hosts="*."`echo ${domainName#*.}`
else
  if [ n${deployEnv} == n"prod" ];then
    domainName="${domainName}"
  elif [ n${deployEnv} == n"pre" ];then
    domainName="s${domainName}"
  elif [ n${deployEnv} == n"dev" ];then
    domainName="t${domainName}"
  fi
  cert_secretName="xueerqin-cert"
  cert_hosts="*.xueerqin.net"
fi

mkdir -p ${Yml_path}
cd ${Yml_path}

cat >${serviceName}-${deployEnv}-ingress-kong.yaml<<EOF
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ${serviceName}-tls
  namespace: ${nameSpaces}
  annotations:
EOF

if [ n${fjfuyu_net} == n"true" ] ;then
cat >>${serviceName}-${deployEnv}-ingress-kong.yaml<<EOF
    cert-manager.io/cluster-issuer: letsencrypt-fjfuyu
    konghq.com/override: https-only
EOF
fi
cat >>${serviceName}-${deployEnv}-ingress-kong.yaml<<EOF
    kubernetes.io/ingress.class: "kong"
spec:
  tls:
  - secretName: ${cert_secretName}
    hosts:
    - "${cert_hosts}"
  rules:
  - host: ${domainName}
    http:
      paths:
      - path: ${urlPath}
        backend:
          serviceName: ${serviceName}
          servicePort: 80
  - host: ${domainName}
    http:
      paths:
      - path: ${urlPath2}
        backend:
          serviceName: ${serviceName2}
          servicePort: 80
EOF
